// ignore_for_file: file_names
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';

class ReorderableListViewPage extends StatefulWidget {
  const ReorderableListViewPage({Key? key}) : super(key: key);

  @override
  _ReorderableListViewPageState createState() => _ReorderableListViewPageState();
}

class _ReorderableListViewPageState extends State<ReorderableListViewPage> {
  List<String> topMovies = [
    "It Happened One Night(1934)",
    "Black Panther(2018)",
    "Citizen Kane(1941)",
    "Parasite (Gisaengchung)(2019)",
    "Avengers: Endgame(2019)",
    "The Wizard of Oz(1939)",
    "Casablanca(1942)",
    "Knives Out(2019)"
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("长按移动交换数据"),
      ),
      body: ReorderableListView(
        onReorder: (int oldIndex, int newIndex) {
          var oldItem = topMovies[oldIndex];
          var newItem = topMovies[newIndex];
          var tempList = topMovies;
          topMovies[oldIndex] = newItem;
          topMovies[newIndex] = oldItem;
          showToast("交换-$newIndex--$oldIndex");
          if (kDebugMode) {
            print("交换-$newIndex--$oldIndex");
          }
          setState(() {
            topMovies = tempList;
          });
        },
        children: getListItems(),
      ),
    );
  }

  List<ListTile> getListItems() =>
      topMovies.asMap().map((i, item) => MapEntry(i, buildTenableListTile(item, i))).values.toList();

  ListTile buildTenableListTile(String item, int index) {
    return ListTile(
      key: ValueKey(item),
      title: Text(item),
      leading: Text("${index + 1}"),
    );
  }
}
